package com.example.blogsystem.model;

import com.example.blogsystem.constant.Constant;
import lombok.Data;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Component;

import javax.annotation.security.DenyAll;

/*
        统一返回结果
     */
@Data
@Component
public class Result<T> {
        private int code; // 状态码 200-成功 -1 失败  -2-未登录
        private String errorInfo; // 错误信息
        private T data; // 后端返回的数据

        //定义返回数据
        public  Result<T> success(T data) {
            Result<T> result = new Result<>();
            result.setCode(Constant.SUCCESS_CODE);
            result.setErrorInfo("");
            result.setData(data);
            return result;
        }
    public  Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(Constant.SUCCESS_CODE);
        return result;
    }

        public Result<T> fail(String errMessage) {
            Result<T> result = new Result<>();
            result.setCode(Constant.FAIL_CODE);
            result.setErrorInfo(errMessage);
            return result;
        }

        public Result<T> fail() {
            Result<T> result = new Result<>();
            result.setCode(Constant.FAIL_CODE);
            result.setErrorInfo("");
            return result;
        }

        public Result<T> unStandard(Integer code) {
            Result<T> result = new Result<>();
            result.setCode(code);
            result.setErrorInfo("");
            return result;
        }

        public Result<T> notLogin() {
            Result<T> result = new Result<>();
            result.setCode(Constant.NOT_LOGIN );
            result.setErrorInfo("用户未登录");
            return result;
        }
}
